"""empty message

Revision ID: b93cf3c1dcdf
Revises:
Create Date: 2024-09-04 15:26:04.310193

"""

from collections.abc import Sequence
from typing import Union

import sqlalchemy as sa

import models
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "b93cf3c1dcdf"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "bot_model_configs",
        sa.Column("id", models.sqlalchemy_types.GUID(), nullable=False),
        sa.Column("bot_id", models.sqlalchemy_types.GUID(), nullable=True),
        sa.Column("provider", sa.String(length=255), nullable=True),
        sa.Column("model_id", sa.String(length=255), nullable=True),
        sa.Column("configs", sa.JSON(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("opening_statement", sa.Text(), nullable=True),
        sa.Column("suggested_questions", sa.Text(), nullable=True),
        sa.Column("suggested_questions_after_answer", sa.Text(), nullable=True),
        sa.Column("speech_to_text", sa.Text(), nullable=True),
        sa.Column("text_to_speech", sa.Text(), nullable=True),
        sa.Column("more_like_this", sa.Text(), nullable=True),
        sa.Column("model", sa.Text(), nullable=True),
        sa.Column("user_input_form", sa.Text(), nullable=True),
        sa.Column("dataset_query_variable", sa.String(length=255), nullable=True),
        sa.Column("pre_prompt", sa.Text(), nullable=True),
        sa.Column("agent_mode", sa.Text(), nullable=True),
        sa.Column("retriever_resource", sa.Text(), nullable=True),
        sa.Column("prompt_type", sa.String(length=255), nullable=True),
        sa.Column("chat_prompt_config", sa.Text(), nullable=True),
        sa.Column("completion_prompt_config", sa.Text(), nullable=True),
        sa.Column("dataset_configs", sa.Text(), nullable=True),
        sa.Column("external_data_tools", sa.Text(), nullable=True),
        sa.Column("file_upload", sa.Text(), nullable=True),
        sa.Column("network", sa.Boolean(), nullable=True),
        sa.PrimaryKeyConstraint("id", name="bot_model_config_pkey"),
    )
    op.create_index("bot_bot_id_idx", "bot_model_configs", ["bot_id"], unique=False)
    op.create_table(
        "bots",
        sa.Column("id", models.sqlalchemy_types.GUID(), nullable=False),
        sa.Column("space_id", models.sqlalchemy_types.GUID(), nullable=True),
        sa.Column("name", sa.String(length=255), nullable=True),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("icon", sa.String(length=255), nullable=True),
        sa.Column("mode", sa.String(length=255), nullable=True),
        sa.Column("bot_model_config_id", models.sqlalchemy_types.GUID(), nullable=True),
        sa.Column("status", sa.String(length=255), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.PrimaryKeyConstraint("id", name="bot_pkey"),
    )
    op.create_index("bot_space_id_idx", "bots", ["space_id"], unique=False)
    op.create_table(
        "conversations",
        sa.Column("id", models.sqlalchemy_types.GUID(), nullable=False),
        sa.Column("bot_id", models.sqlalchemy_types.GUID(), nullable=True),
        sa.Column("bot_model_config_id", models.sqlalchemy_types.GUID(), nullable=True),
        sa.Column("model_provider", sa.String(length=255), nullable=True),
        sa.Column("override_model_configs", sa.Text(), nullable=True),
        sa.Column("model_id", sa.String(length=255), nullable=True),
        sa.Column("name", sa.String(length=255), nullable=True),
        sa.Column("mode", sa.String(length=255), nullable=True),
        sa.Column("summary", sa.Text(), nullable=True),
        sa.Column("inputs", sa.JSON(), nullable=True),
        sa.Column("docs", sa.JSON(), nullable=True),
        sa.Column("introduction", sa.Text(), nullable=True),
        sa.Column("system_instruction", sa.Text(), nullable=True),
        sa.Column("system_instruction_tokens", sa.Integer(), nullable=True),
        sa.Column("status", sa.String(length=255), nullable=True),
        sa.Column("invoke_from", sa.String(length=255), nullable=True),
        sa.Column("from_source", sa.String(length=255), nullable=True),
        sa.Column("from_user_id", models.sqlalchemy_types.GUID(), nullable=True),
        sa.Column("read_at", sa.DateTime(), nullable=True),
        sa.Column("read_user_id", models.sqlalchemy_types.GUID(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("is_deleted", sa.Boolean(), nullable=True),
        sa.PrimaryKeyConstraint("id", name="conversation_pkey"),
    )
    op.create_table(
        "message_agent_thoughts",
        sa.Column("id", models.sqlalchemy_types.GUID(), nullable=False),
        sa.Column("message_id", models.sqlalchemy_types.GUID(), nullable=True),
        sa.Column("message_chain_id", models.sqlalchemy_types.GUID(), nullable=True),
        sa.Column("position", sa.Integer(), nullable=True),
        sa.Column("thought", sa.Text(), nullable=True),
        sa.Column("tool", sa.Text(), nullable=True),
        sa.Column("tool_input", sa.Text(), nullable=True),
        sa.Column("observation", sa.Text(), nullable=True),
        sa.Column("tool_process_data", sa.Text(), nullable=True),
        sa.Column("tool_time_cost", sa.Float(), nullable=True),
        sa.Column("message", sa.Text(), nullable=True),
        sa.Column("message_token", sa.Integer(), nullable=True),
        sa.Column("answer", sa.Text(), nullable=True),
        sa.Column("answer_token", sa.Integer(), nullable=True),
        sa.Column("tokens", sa.Integer(), nullable=True),
        sa.Column("currency", sa.String(), nullable=True),
        sa.Column("latency", sa.Float(), nullable=True),
        sa.Column("created_by_role", sa.String(), nullable=True),
        sa.Column("created_by", models.sqlalchemy_types.GUID(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.PrimaryKeyConstraint("id", name="message_agent_thought_pkey"),
    )
    op.create_index(
        "message_agent_thought_message_chain_id_idx",
        "message_agent_thoughts",
        ["message_chain_id"],
        unique=False,
    )
    op.create_index(
        "message_agent_thought_message_id_idx",
        "message_agent_thoughts",
        ["message_id"],
        unique=False,
    )
    op.create_table(
        "message_chains",
        sa.Column("id", models.sqlalchemy_types.GUID(), nullable=False),
        sa.Column("message_id", models.sqlalchemy_types.GUID(), nullable=True),
        sa.Column("type", sa.String(length=255), nullable=True),
        sa.Column("input", sa.Text(), nullable=True),
        sa.Column("output", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.PrimaryKeyConstraint("id", name="message_chain_pkey"),
    )
    op.create_index("message_chain_message_id_idx", "message_chains", ["message_id"], unique=False)
    op.create_table(
        "users",
        sa.Column("id", models.sqlalchemy_types.GUID(), nullable=False),
        sa.Column("username", sa.String(length=255), nullable=True),
        sa.Column("email", sa.String(length=255), nullable=True),
        sa.Column("password", sa.String(length=255), nullable=True),
        sa.Column("status", sa.String(length=16), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.PrimaryKeyConstraint("id", name="user_pkey"),
    )
    op.create_index("user_email_idx", "users", ["email"], unique=False)
    op.create_table(
        "workspace_users",
        sa.Column("id", models.sqlalchemy_types.GUID(), nullable=False),
        sa.Column("workspace_id", models.sqlalchemy_types.GUID(), nullable=True),
        sa.Column("user_id", models.sqlalchemy_types.GUID(), nullable=True),
        sa.Column("current", sa.Boolean(), nullable=True),
        sa.Column("role", sa.String(length=16), nullable=True),
        sa.Column("invited_by", models.sqlalchemy_types.GUID(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.PrimaryKeyConstraint("id", name="workspace_users_pkey"),
        sa.UniqueConstraint("workspace_id", "user_id", name="unique_workspace_users"),
    )
    op.create_index(
        "workspace_users_space_id_idx",
        "workspace_users",
        ["workspace_id"],
        unique=False,
    )
    op.create_index("workspace_users_user_id_idx", "workspace_users", ["user_id"], unique=False)
    op.create_table(
        "workspaces",
        sa.Column("id", models.sqlalchemy_types.GUID(), nullable=False),
        sa.Column("name", sa.String(length=255), nullable=True),
        sa.Column("status", sa.String(length=255), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.PrimaryKeyConstraint("id", name="workspace_pkey"),
    )
    op.create_table(
        "file",
        sa.Column("file_id", sa.String(length=255), nullable=False),
        sa.Column("file_name", sa.String(length=255), nullable=True),
        sa.Column("user_id", models.sqlalchemy_types.GUID(), nullable=True),
        sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("file_id", name="file_pkey"),
    )
    op.create_index("idx_file_id", "file", ["file_id"], unique=False)
    op.create_table(
        "knowledge",
        sa.Column("collection_name", sa.String(length=255), nullable=False),
        sa.Column("user_id", models.sqlalchemy_types.GUID(), nullable=True),
        sa.Column("knowledge_name", sa.String(length=255), nullable=True),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("embedding_model", sa.String(length=255), nullable=True),
        sa.Column("index_params", sa.JSON(), nullable=True),
        sa.Column("similarity_threshold", sa.Float(), nullable=True),
        sa.Column("create_at", sa.DateTime(), nullable=True),
        sa.Column("update_at", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("collection_name"),
    )
    op.create_index("knowledge_name", "knowledge", ["knowledge_name"], unique=False)
    op.create_index("knowledge_user_id", "knowledge", ["user_id"], unique=False)
    op.create_table(
        "messages",
        sa.Column("id", models.sqlalchemy_types.GUID(), nullable=False),
        sa.Column("bot_id", models.sqlalchemy_types.GUID(), nullable=True),
        sa.Column("model_provider", sa.String(length=255), nullable=True),
        sa.Column("model_id", sa.String(length=255), nullable=True),
        sa.Column("override_model_configs", sa.Text(), nullable=True),
        sa.Column("conversation_id", models.sqlalchemy_types.GUID(), nullable=True),
        sa.Column("inputs", sa.JSON(), nullable=True),
        sa.Column("query", sa.Text(), nullable=True),
        sa.Column("message", sa.JSON(), nullable=True),
        sa.Column("files", sa.JSON(), nullable=True),
        sa.Column("message_tokens", sa.Integer(), nullable=True),
        sa.Column("answer", sa.Text(), nullable=True),
        sa.Column("answer_tokens", sa.Integer(), nullable=True),
        sa.Column("provider_response_latency", sa.Float(), nullable=True),
        sa.Column("status", sa.String(length=255), nullable=True),
        sa.Column("error", sa.Text(), nullable=True),
        sa.Column("message_metadata", sa.Text(), nullable=True),
        sa.Column("invoke_from", sa.String(length=255), nullable=True),
        sa.Column("from_source", sa.String(length=255), nullable=True),
        sa.Column("from_user_id", models.sqlalchemy_types.GUID(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("agent_based", sa.Boolean(), nullable=True),
        sa.ForeignKeyConstraint(
            ["conversation_id"],
            ["conversations.id"],
        ),
        sa.PrimaryKeyConstraint("id", name="message_pkey"),
    )
    op.create_index("message_bot_id_idx", "messages", ["bot_id", "created_at"], unique=False)
    op.create_index("message_conversation_id_idx", "messages", ["conversation_id"], unique=False)
    op.create_index(
        "message_user_idx",
        "messages",
        ["bot_id", "from_source", "from_user_id"],
        unique=False,
    )
    op.create_table(
        "models",
        sa.Column("id", models.sqlalchemy_types.GUID(), nullable=False),
        sa.Column("model_name", sa.String(), nullable=True),
        sa.Column("ollama_model_name", sa.String(), nullable=True),
        sa.Column("digest", sa.String(), nullable=True),
        sa.Column("source", sa.String(), nullable=True),
        sa.Column("description", sa.String(), nullable=True),
        sa.Column("size", sa.BIGINT(), nullable=True),
        sa.Column("model_fmt", sa.String(), nullable=True),
        sa.Column("quantization_level", sa.String(), nullable=True),
        sa.Column("modelfile", sa.String(), nullable=True),
        sa.Column("is_generation", sa.Boolean(), nullable=True),
        sa.Column("is_embeddings", sa.Boolean(), nullable=True),
        sa.Column("use_xunlei", sa.Boolean(), nullable=True),
        sa.Column(
            "download_status",
            sa.Enum(
                "DOWNLOAD_WAITING",
                "DOWNLOADING",
                "DOWNLOAD_COMPLETE",
                "CONVERT_COMPLETE",
                "IMPORT_COMPLETE",
                "ALL_COMPLETE",
                "DOWNLOAD_FAILED",
                "CONVERT_FAILED",
                "IMPORT_FAILED",
                "NOT_AVAILABLE",
                name="downloadstatus",
            ),
            nullable=True,
        ),
        sa.Column("download_progress", sa.Integer(), nullable=True),
        sa.Column("process_message", sa.String(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("created_by", models.sqlalchemy_types.GUID(), nullable=True),
        sa.Column("updated_by", models.sqlalchemy_types.GUID(), nullable=True),
        sa.ForeignKeyConstraint(
            ["created_by"],
            ["users.id"],
        ),
        sa.ForeignKeyConstraint(
            ["updated_by"],
            ["users.id"],
        ),
        sa.PrimaryKeyConstraint("id", name="model_pkey"),
        sa.UniqueConstraint("model_name", name="unique_model_key"),
        sa.UniqueConstraint("ollama_model_name", name="unique_ollama_key"),
    )
    op.create_table(
        "datasets",
        sa.Column("id", models.sqlalchemy_types.GUID(), nullable=False),
        sa.Column("space_id", models.sqlalchemy_types.GUID(), nullable=True),
        sa.Column("bot_id", models.sqlalchemy_types.GUID(), nullable=True),
        sa.Column("collection_name", sa.String(length=255), nullable=True),
        sa.Column("user_id", models.sqlalchemy_types.GUID(), nullable=True),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("embedding_model", sa.String(length=255), nullable=True),
        sa.Column("permission", sa.BIGINT(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(["bot_id"], ["bots.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["collection_name"], ["knowledge.collection_name"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["space_id"], ["workspaces.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id", name="dataset_pkey"),
    )
    op.create_index("dataset_bot_idx", "datasets", ["bot_id"], unique=False)
    op.create_index("dataset_collection_name", "datasets", ["collection_name"], unique=False)
    op.create_index("dataset_space_idx", "datasets", ["space_id"], unique=False)
    op.create_table(
        "document",
        sa.Column("partition_name", sa.String(length=255), nullable=False),
        sa.Column("collection_name", sa.String(length=255), nullable=True),
        sa.Column("file_id", sa.String(length=255), nullable=True),
        sa.Column("file_name", sa.String(length=255), nullable=True),
        sa.Column("file_url", sa.String(length=255), nullable=True),
        sa.Column("file_type", sa.String(length=255), nullable=True),
        sa.Column("content_length", sa.BIGINT(), nullable=True),
        sa.Column("content", sa.Text(), nullable=True),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("progress", sa.Float(), nullable=True),
        sa.Column("document_status", sa.BIGINT(), nullable=True),
        sa.Column("msg", sa.Text(), nullable=True),
        sa.Column("create_at", sa.DateTime(), nullable=True),
        sa.Column("update_at", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(["collection_name"], ["knowledge.collection_name"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["file_id"], ["file.file_id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("partition_name"),
    )
    op.create_index("document_collection_name", "document", ["collection_name"], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index("document_collection_name", table_name="document")
    op.drop_table("document")
    op.drop_index("dataset_space_idx", table_name="datasets")
    op.drop_index("dataset_collection_name", table_name="datasets")
    op.drop_index("dataset_bot_idx", table_name="datasets")
    op.drop_table("datasets")
    op.drop_table("models")
    op.drop_index("message_user_idx", table_name="messages")
    op.drop_index("message_conversation_id_idx", table_name="messages")
    op.drop_index("message_bot_id_idx", table_name="messages")
    op.drop_table("messages")
    op.drop_index("knowledge_user_id", table_name="knowledge")
    op.drop_index("knowledge_name", table_name="knowledge")
    op.drop_table("knowledge")
    op.drop_index("idx_file_id", table_name="file")
    op.drop_table("file")
    op.drop_table("workspaces")
    op.drop_index("workspace_users_user_id_idx", table_name="workspace_users")
    op.drop_index("workspace_users_space_id_idx", table_name="workspace_users")
    op.drop_table("workspace_users")
    op.drop_index("user_email_idx", table_name="users")
    op.drop_table("users")
    op.drop_index("message_chain_message_id_idx", table_name="message_chains")
    op.drop_table("message_chains")
    op.drop_index("message_agent_thought_message_id_idx", table_name="message_agent_thoughts")
    op.drop_index(
        "message_agent_thought_message_chain_id_idx",
        table_name="message_agent_thoughts",
    )
    op.drop_table("message_agent_thoughts")
    op.drop_table("conversations")
    op.drop_index("bot_space_id_idx", table_name="bots")
    op.drop_table("bots")
    op.drop_index("bot_bot_id_idx", table_name="bot_model_configs")
    op.drop_table("bot_model_configs")
    # ### end Alembic commands ###
